package com.kgis.fm.business.management.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kgis.fm.business.access.service.ConnectivityAccessService;
import com.kgis.fm.business.management.service.ConnectivityManagementService;
import com.kgis.fm.business.model.Connectivity;
import com.kgis.fm.business.model.FeatureConnectivity;
import com.kgis.fm.business.model.KeyObject;
/**
 * 
 * @author zhxiaomiao
 *
 */
public class ConnectivityManagementServiceImpl implements ConnectivityManagementService{
	private static final transient Log log = LogFactory.getLog(ConnectivityManagementServiceImpl.class);
	private ConnectivityAccessService<Connectivity> connectivityAccessService;
	
	@Override
	public Connectivity findByFnoFid(int fno, long fid) {
		
		return connectivityAccessService.findByFnoFid(fno, fid);
	}

	@Override
	public List<Connectivity> findRelatedFeaturesByFnoFid(int fno, long fid) {
		return connectivityAccessService.findRelatedFeaturesByFnoFid(fno, fid);
	}

	public ConnectivityAccessService<Connectivity> getConnectivityAccessService() {
		return connectivityAccessService;
	}

	public void setConnectivityAccessService(ConnectivityAccessService<Connectivity> connectivityAccessService) {
		this.connectivityAccessService = connectivityAccessService;
	}

	@Override
	public List<FeatureConnectivity> findFeatureConnectivityList(List<KeyObject> keyObejcts) {
		if(keyObejcts==null || keyObejcts.size()==0){
			return null;
		}
		List<FeatureConnectivity>result=new ArrayList<FeatureConnectivity>();
		for (KeyObject keyObject : keyObejcts) {
			FeatureConnectivity featureConnectivity=new FeatureConnectivity();
			featureConnectivity.setFno(keyObject.getFno());
			featureConnectivity.setFid(keyObject.getFid());
			Connectivity connectivity = findByFnoFid(keyObject.getFno(), keyObject.getFid());
			if(connectivity==null){
				if (log.isInfoEnabled()) {
					log.info("根据fno、fid查询connectivity_n结果为空："+"fno:"+keyObject.getFno()+" fid:"+keyObject.getFid());
				}
				continue;
			}
			featureConnectivity.setConnectivity(connectivity);
			long node1Value = connectivity.getNode1();
			long node2Value = connectivity.getNode2();
			if (node1Value == 0 && node2Value == 0) {
				return null;
			}
			List<Connectivity> resultConnectivity=connectivityAccessService.findRelatedFeatureByNodes(node1Value,node2Value,keyObject.getFid());
			//构造数据结构
			List<Connectivity>node1List=new ArrayList<Connectivity>();
			List<Connectivity>node2List=new ArrayList<Connectivity>();
			for (Connectivity entity : resultConnectivity) {
				if(entity.getNode1()==node1Value || entity.getNode2()==node1Value){
					node1List.add(entity);
				}
				if(entity.getNode1()==node2Value || entity.getNode2()==node2Value){
					node2List.add(entity);
				}
			}
			featureConnectivity.setNode1ConnectivityList(node1List);
			featureConnectivity.setNode2ConnectivityList(node2List);
			result.add(featureConnectivity);
		}

		return result;
	}

	

	
}
